define(function(require, exports, module) {
    var Q = require("Q");
    var template = require("template");
    var layer = require("layer");
    var Ajax = require("ajax");
    var Router = function(){
        this.init.apply(this,arguments);
    };
    Router.prototype = {
        constructor: Router,
        data:{},  //模板渲染用到的数据
        init:function(options){
            template.config("cache", false);
            //显示当前用户的信息
			$(".user").text($.cookie("userName")||"");
			$(".company").text($.cookie("companyName")||"");
			$(".depart").text($.cookie("departName")||"");
            //注册路由
            this.regRouter();
            var targetUrl = "admin/home";
            var href = window.location.href;
            //路由配置
            Q.init({
                key:"!",
                index:targetUrl,
                pop:function(){ /* 每次 URL 变更时发生回调 */
                    
                }
            });
			if(href.indexOf("kickout")>-1||href.indexOf("forceLogout")>-1){
				layer.msg("您已被踢出，请重新登陆！",{time: 3000},function(){
					window.location.href = localhost + 'views/index.jsp#!index/login';
                });
            }else if(href.indexOf("index.jsp")>-1){
				window.location.href = localhost + 'views/index.jsp#!index/login';
            }
//            var t=this;
//            t.adjustLoginOrNot();
        },
        /**
         * 注册路由 注：先注册后初始化
         * */
        regRouter:function(){
            var t = this;
            /**
             * 页面链接实例： index.jsp#!admin
             * */

            Q.reg("admin",function(moduleName,params){
                if(!moduleName){return alert("url传入参数不正确，请确认您访问的地址!");}
                var oPara = {moduleName:moduleName}; //附加其他参数
                if(params && params.indexOf(':') > -1) {
                    params.replace(/(\w+)\s*:\s*([\w-]+)/g, function(a, b, c) {
                        b && (oPara[b] = c);
                    });
                } else {
                    oPara = params;
                }
                
                //权限设置
                if($.cookie("roleEnum") == "admin_super"){
                	$(".superAdminRight a").addClass("current");
                	$(".superAdminRight").siblings().remove();
    				if(moduleName!=="set"){
    					moduleName = "set";
    				}
                }else if ($.cookie("roleEnum") == "user_com") {
    				if(moduleName=="submit"||moduleName=="submit_pass"){
    					moduleName = "submit_audit";
    				}
    			}else if($.cookie("roleEnum") == "user_simple"){
    				if(moduleName=="submit_audit"){
    					moduleName = "submit_pass";
    				}
    			}else if($.cookie("roleEnum") == "admin_com"){
    				if(moduleName=="submit"||moduleName=="submit_audit"){
    					moduleName = "submit_pass";
    				}
				}
                
                require.async(["css/admin/"+moduleName+".css","templates/admin/"+moduleName+".html"],function(undefined,tpl){
                	$("#rightConBox").html(tpl);
                    require.async("js/modules/admin/"+moduleName+".js",function(Module){
                        new Module(oPara);
                        t.menuLuoDianFun();//公用导航落点
                    });
                });
            });
            Q.reg("index",function(moduleName,params){
                if(!moduleName){return alert("url传入参数不正确，请确认您访问的地址!");}
                var oPara = {moduleName:moduleName}; //附加其他参数
                if(params && params.indexOf(':') > -1) {
                    params.replace(/(\w+)\s*:\s*([\w-]+)/g, function(a, b, c) {
                        b && (oPara[b] = c);
                    });
                } else {
                    oPara = params;
                }
                require.async(["css/index/"+moduleName+".css","templates/index/"+moduleName+".html"],function(undefined,tpl){
                    $("#centerBox").html(tpl);
                    require.async("js/modules/index/"+moduleName+".js",function(Module){
                        new Module(oPara);
                    });
                });
            });
        },
        menuLuoDianFun:function(){
        	var wHref = location.href;
        	var t=this;
        	 $("#leftConBox li a").each(function(index){
                 var thisHref = $(this).attr("href");
                 if(wHref.indexOf(thisHref)>-1){
                     var $parA = $(this);
                     if(!$parA.hasClass("current")){
                         $("#leftConBox li a").removeClass("current");
                     }
                     $parA.addClass("current");
//                     t.adjustLoginOrNot();
                 }
            });
        },
        adjustLoginOrNot:function(){
            var href = window.location.href;
        	if(href.indexOf("admin.jsp")>-1){
        		
            	Ajax.ajax({
                  url:localhost + "checkLogin",
                    success:function(data){
                    	if(data.resultDesc=='0'){//未登录
                    		window.location.href = localhost + 'views/index.jsp#!index/login';
                    		return false;
                    	}else{
                    		 Ajax.ajax({
                               url:localhost + "user/selectByPrimaryKey",
                                 success:function(data){
                                     $("#userCount").html(data.data.userAccount);
                                     if(data.data.userPermission==1){
                                         $("#userAdmin").css('display','block');
                                     }else{
                                         $("#userAdmin").css('display','none');
                                     }
                                 }
                             });
                    	}
                    }
               });
			}else if(href.indexOf("index.jsp")>-1){
				window.location.href = localhost + 'views/index.jsp#!index/login';
            }
        }
    };
    module.exports = Router;

});
